//
//  UserVC.m
//  UHealth
//
//  Created by gjtxz on 14/12/30.
//  Copyright (c) 2014年 gjtx. All rights reserved.
//

#import "UserVC.h"
#import "AppDelegate.h"
#import "PersonalVC.h"
#import "OrderVC.h"
#import "AboutUVC.h"
#import "SetVC.h"
#import "LeftUserCell.h"
#import "DBManage.h"
#import "UIImageView+WebCache.h"

@interface UserVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)  UITableView        *userTable;
@property (nonatomic, strong)  NSMutableArray     *userArray;

@end

@implementation UserVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self initView];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) initView
{
    
//    self.view.backgroundColor = kBlackColor;
    self.navigationController.navigationBarHidden = YES;
    
//    _userArray = [NSMutableArray arrayWithObjects:@{@"img":@"user",@"title":@"Tang",@"detail":@"BT Disconnected"},@{@"title":@"My Temperature"},@{@"title":@"My Order"},@{@"title":@"About Device"},@{@"title":@"Setting Up"},nil];
    
    if (kNetID) {
        _userArray = [NSMutableArray arrayWithObjects:@{@"img":@"血糖1",@"title":kLocal(@"now_tem")},@{@"img":@"订单1",@"title":kLocal(@"myOrder")},@{@"img":@"关于1",@"title":kLocal(@"dynamic_tem")},@{@"img":@"设置1",@"title":kLocal(@"setting")},nil];
        
    }else{
        _userArray = [NSMutableArray arrayWithObjects:@{@"img":@"血糖1",@"title":kLocal(@"now_tem")},@{@"img":@"订单1",@"title":kLocal(@"myOrder")},@{@"img":@"关于1",@"title":kLocal(@"dynamic_tem")},@{@"img":@"设置1",@"title":kLocal(@"setting")},@{@"img":@"设置1",@"title":kLocal(@"login_register")},nil];
    }
    
    
    
    _userTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 200, 60+40*4 +100) style:UITableViewStylePlain];
    _userTable.delegate = self;
    _userTable.dataSource = self;
    _userTable.opaque = NO;
    _userTable.backgroundColor = [UIColor clearColor];
    _userTable.backgroundView = nil;
//    _userTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _userTable.bounces = NO;
    _userTable.tableFooterView = [[UIView alloc] init];//不显示下面的分割线
    [_userTable setSeparatorInset:UIEdgeInsetsZero];
    [self setContentViewInSideBar:_userTable];
    
    [[DBManage sharedManager] setValue:[[UserObj alloc]init] forKey:kFirstUser];
    [[DBManage sharedManager] addObserver:self forKeyPath:kFirstUser options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];



}

//- (void)getBlueToothConnectedNoti
//{
//    _userArray = [NSMutableArray arrayWithObjects:@{@"img":@"user",@"title":@"Tang",@"detail":@"BT Connected"},@{@"title":@"My Temperature"},@{@"title":@"My Order"},@{@"title":@"About Device"},@{@"title":@"Setting Up"},nil];
//    [self.userTable reloadData];
//}
//
//- (void)getBlueToothDisConnectedNoti
//{
//    _userArray = [NSMutableArray arrayWithObjects:@{@"img":@"user",@"title":@"Tang",@"detail":@"BT Disconnected"},@{@"title":@"My Temperature"},@{@"title":@"My Order"},@{@"title":@"About Device"},@{@"title":@"Setting Up"},nil];
//    [self.userTable reloadData];
//}

-(void)dealloc{
    [[DBManage sharedManager] removeObserver:self forKeyPath:kFirstUser];
}

#pragma mark - KVO 回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:kFirstUser])
    {
        [_userTable reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return _userArray.count+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 150;
    }
    
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row!=0) {
        NSDictionary *dict  = _userArray[indexPath.row - 1];
        static NSString *ID =@"cellId";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[dict objectForKey:@"img"]];
        cell.textLabel.text = [dict objectForKey:@"title"];
        [cell.textLabel setTextColor:[UIColor whiteColor]];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    
    }else{
        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LeftUserCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([LeftUserCell class])];
        LeftUserCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LeftUserCell class]) forIndexPath:indexPath];
        UserObj *u = [[DBManage sharedManager]getUserWithID:0];
        if (u) {
            cell.Title.text = u.name;
            [cell.Img sd_setImageWithURL:[NSURL URLWithString:u.headImg] placeholderImage:[UIImage imageNamed:@"user"]];
        }
        
        cell.backgroundColor = [UIColor clearColor];
        return cell;

    }
    
    return nil;
    
 
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:{
            PersonalVC *per = [[PersonalVC alloc]init];
            [self presentViewController:per animated:YES completion:nil];
        }
            break;
        case 1:{
            [self dismiss];
        }
            break;
        case 2:{
            [self presentViewController:[[OrderVC alloc]init] animated:YES completion:nil];
        }
            break;
        case 3:{
            [self presentViewController:[[AboutUVC alloc]init] animated:YES completion:nil];
        }
            break;
        case 4:{
            
            [self presentViewController:[[SetVC alloc]init] animated:YES completion:nil];
        }
            break;
        case 5:{
            AppDelegate *de = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [de changRootVCToLaunch];
        }
            
        default:
            break;
    }

}



@end
